package com.test360.controller;

import cn.thinkjoy.cloudstack.dynconfig.DynConfigClient;
import cn.thinkjoy.cloudstack.dynconfig.DynConfigClientFactory;
import com.alibaba.fastjson.JSONObject;
import com.qtone.open.api.uic.IUICScore;
import com.qtone.open.api.uic.dto.KsPcDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;

/**
 * Created by huangshengqing on 2016/3/14.
 *
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    IUICScore iuicScore;

    @ResponseBody
    @RequestMapping(value = "/score")
    public Object score(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String operId="43b7442a88e411e5be87fa163e33daaa";
        List<KsPcDto> list = iuicScore.getKspc(operId, "weixiao", "4415024030");
//        map.put("list", list);
        String uid="f794fb88f5a111e58cc6fa163e37f8a9";
//        map.put("score",iuicScore.getScore(operId,"weixiao","c30899e1-e5f2-40f5-b795-ed74f3443f9a",uid));
        return iuicScore.getScore(operId,"weixiao","c30899e1-e5f2-40f5-b795-ed74f3443f9a",uid);
//        return map;

    }

    @ResponseBody
    @RequestMapping(value = "/writeFile")
    public Object writeFile(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        FileOutputStream out = null;

        try {
            String fileName = request.getSession().getServletContext().getRealPath("/static/");
            File file = new File(fileName);
            if (!file.exists()) {
                file.mkdir();
            }
            fileName = fileName + "aa.json";
            System.out.println(fileName);
            out = new FileOutputStream(new File(fileName));

            List<TestUser>userLst=new ArrayList<TestUser>();
            for(int i=0;i<100;i++){
                TestUser tu=new TestUser();
                tu.setId(i);
                tu.setPassword("aa"+i);
                tu.setName("测试"+i);
                tu.setCreateDate(new Date());
                userLst.add(tu);

            }

            String msg = JSONObject.toJSONString(userLst);
            out.write(msg.getBytes());
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return map;

    }

    @ResponseBody
    @RequestMapping(value = "/readFile")
    public Object readFile(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        String path = "E:\\codes\\xhck\\jishu2_team1\\webDemo\\classes\\artifacts\\web-war\\exploded\\web-war-0.0.1-SNAPSHOT.war\\static\\aa.json";
        try {
            FileInputStream fis = new FileInputStream(new File(path));
            byte[] b = new byte[fis.available()];//新建一个字节数组
            fis.read(b);//将文件中的内容读取到字节数组中
            fis.close();
            String str2 = new String(b);//再将字节数组中的内容转化成字符串形式输出
            System.out.println(str2);
            List<TestUser>userLst= JSONObject.parseArray(str2,TestUser.class);
            map.put("list",userLst);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/test01")
    public Object test01(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        DynConfigClient dynConfigClient = DynConfigClientFactory.getClient();


        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/login")
    public Object login(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> parames) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        logger.info(parames.toString());
        map.put("success", true);
        map.put("msg", "登录成功");

        return map;
    }
}
